package zsd.reiview.experiment.crf;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;


import zsd.reiview.utility.StringUtility;
import zsd.review.split.IctSpliting;

import com.aliasi.chunk.Chunking;
import com.aliasi.tokenizer.Tokenizer;
import com.aliasi.tokenizer.TokenizerFactory;
import com.aliasi.util.AbstractExternalizable;
import com.aliasi.util.Strings;

/**
 * @Title: IctSplitTokenFactory.java
 * @Package zsd.reiview.experiment.crf
 * @Description: TODO(添加描述)
 * @author 朱圣代
 * @date 2011-10-31 下午02:37:04
 * @version V1.0
 */
public class IctSplitTokenizerFactory implements TokenizerFactory,Serializable {

	  
	       /**  
	        * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)  
	        */  
	    
	private static final long serialVersionUID = -3403268518808954113L;
	/**
	 * @Title: main
	 * @Description:
	 * @param @param args
	 * @return void
	 * @throws
	 * @date 2011-10-31 下午02:37:04
	 */
	public static IctSpliting ictSpliting = new IctSpliting();

	public IctSplitTokenizerFactory() {
		// ictSpliting.Init();
	}

	public static void main(String[] args) {

	}

	@Override
	public Tokenizer tokenizer(char[] ch, int start, int length) {
		//String  str= StringUtility.chArray2Str(ch,false,' ').toString();
		/*List<String> toks = ictSpliting.splitToWords(str);
		if( toks == null )
			return null;*/
		Tokenizer tokenizer = new SplitWordTokenizer(ch, start, length);
		return tokenizer;
	}

	public static class SplitWordTokenizer extends Tokenizer {
public static IctSpliting ictSpliting = new IctSpliting();
		private final char[] mChars;
		private final int mLastPosition;
		private final int mStartPosition;

		private int mPosition;
		private int mTokenStart;
		private int mLastTokenIndex;

		private int mLastTokenStartPosition = -1;
		private int mLastTokenEndPosition = -1;
		
		private List<String> mtoks;
		private int[] mtokFirstPos;
		private int mtokenNum;

		public SplitWordTokenizer(List<String> toks) {
			this(StringUtility.list2str(toks));
		}

		public SplitWordTokenizer(char[] ch, int offset, int length) {
			if (offset < 0 || offset + length > ch.length) {
				String msg = "Illegal slice." + " cs.length=" + ch.length + " offset=" + offset + " length=" + length;
				throw new IllegalArgumentException(msg);
			}
			mChars = ch;
			//System.out.println(ch);
			mPosition = offset;
			mLastPosition = offset + length;
			mTokenStart = -1;
			mLastTokenIndex = -1;
			mStartPosition = offset;
			
			
			mtoks = ictSpliting.splitToWords(new String(ch));
			mtokFirstPos = ictSpliting.tokenFirstPos;
			mtokenNum = 0;
		}


		public SplitWordTokenizer(String chars) {
			this(chars.toCharArray(), 0, chars.length());
		}


		public SplitWordTokenizer(StringBuilder chars) {
			this(chars.toString());
		}

		@Override
		public int lastTokenStartPosition() {
			return mLastTokenStartPosition;
		}

		@Override
		public int lastTokenEndPosition() {
			return mLastTokenEndPosition;
		}

		@Override
		public String nextWhitespace() {
			StringBuilder sb = new StringBuilder();
			while (hasMoreCharacters() && Character.isWhitespace(currentChar())) {
				sb.append(currentChar());
				++mPosition;
			}
			return sb.toString();
		}

	

		@Override
		public String nextToken() {
			//skipWhitespace();
			if (!hasMoreCharacters())
				return null;
			mTokenStart = mPosition;
			++mLastTokenIndex;
			String tok =  mtoks.get(mtokenNum);
			mPosition += tok.length();
				
				mtokenNum++;
			return currentToken(); 
		}


		private boolean hasMoreCharacters() {
			return ( mPosition < mLastPosition) &&( mtokenNum < mtoks.size());
		}

		private char currentChar() {
			return mChars[mPosition];
		}

	
		private boolean currentCharEquals(char c) {
			return hasMoreCharacters() && currentChar() == c;
		}

	
		private void skipWhitespace() {
			while (hasMoreCharacters() && Character.isWhitespace(currentChar()))
				++mPosition;
		}
		
	

		private String currentToken() {
			int length = mPosition - mTokenStart;
			mLastTokenStartPosition = mTokenStart - mStartPosition;
			mLastTokenEndPosition = mLastTokenStartPosition + length;
			return new String(mChars, mTokenStart, length);
		}
	}
	
	private Object writeReplace() {
		return new Externalizer<String>(this);
	}

	private static class Externalizer<F> extends AbstractExternalizable {
		
		private final IctSplitTokenizerFactory mictSplitTokenizerFactory;

		public Externalizer(IctSplitTokenizerFactory ictSplitTokenizerFactory) {
			this.mictSplitTokenizerFactory = ictSplitTokenizerFactory;
		}

		public Externalizer() {
			this(null);
		}

		@Override
		public Object read(ObjectInput in) throws IOException, ClassNotFoundException {
			IctSplitTokenizerFactory ictSplitTokenizerFactory = new IctSplitTokenizerFactory();
			return ictSplitTokenizerFactory;
		}

		@Override
		public void writeExternal(ObjectOutput out) throws IOException {

		}
	}
}
